<template>
  <div>
    <h1>{{id ? '编辑' : '新建'}}商品</h1>
    <el-card class="box-card">
      <el-form @submit.native.prevent="saveGoods" ref="form" :model="goods" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="goods.name"></el-input>
        </el-form-item>
        <el-form-item label="商品类型">
          <el-select v-model="goods.category" placeholder="请选择">
            <el-option
              v-for="item in categories"
              :key="item._id"
              :label="item.name"
              :value="item._id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品图片">
          <el-button size="small" type="primary" plain @click="goods.pic.push({})">
            <i class="el-icon-plus"></i>
            添加图片
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <el-row :md="24" v-for="(item, i) in goods.pic" :key="i">
              <el-form-item label=" " style="margin-top:0.5rem;">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadUrl"
                  :headers = getAuthHeaders()
                  :show-file-list="false"
                  :on-success="res => $set(item, 'url', res.url)"
                  accept="image/*"
                >
                  <img v-if="item.url" :src="item.url" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item>
                <el-button
                  style="margin:0.2rem 0 60px 135px;"
                  type="danger"
                  @click="goods.pic.splice(i, 1)"
                >删除</el-button>
              </el-form-item>
            </el-row>
          </el-row>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="goods.originalPrice"></el-input>
        </el-form-item>
        <el-form-item label="现价">
          <el-input v-model="goods.presentPrice"></el-input>
        </el-form-item>
        <el-form-item label="规格">
          <el-input v-model="goods.specifications"></el-input>
        </el-form-item>
        <el-form-item label="库存">
          <el-input v-model="goods.stock"></el-input>
        </el-form-item>
        <el-form-item label="商品描述" autosize>
          <el-input type="textarea" v-model="goods.desc"></el-input>
        </el-form-item>
        <el-form-item label="详情">
          <vue-editor useCustomImageHandler @image-added="handleImageAdded" v-model="goods.detail"></vue-editor>
        </el-form-item>
        <el-form-item>
          <el-radio v-model="goods.state" label="1" border>上架</el-radio>
          <el-radio v-model="goods.state" label="0" border>下架</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" native-type="submit">保存</el-button>
          <el-button @click="$router.push('/goods/list')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { VueEditor } from "vue2-editor";

export default {
  components: {
    VueEditor
  },
  props: {
    id: {}
  },
  data() {
    return {
      goods: {
        pic: []
      },
      categories: [],
      limit: 4,
      fileList: [],
    };
  },
  methods: {
    async fetch() {
      const goodsResult = await this.$http.get(`rest/goods/${this.id}`);
      this.goods = Object.assign({}, this.goods, goodsResult.data);
    },
    async fetchCategory() {
      const categoryResult = await this.$http.get("rest/categories");
      this.categories = categoryResult.data;
    },
    async handleImageAdded(file, Editor, cursorLocation, resetUploader) {
      const formData = new FormData();
      formData.append("file", file);
      const res = await this.$http.post('/upload', formData)
      console.log(res)
      Editor.insertEmbed(cursorLocation, "image", res.data.url);
      resetUploader();
    },
    async saveGoods() {
      if (this.id) {
        await this.$http.put(`rest/goods/${this.id}`, this.goods);
      } else {
        await this.$http.post("rest/goods", this.goods);
      }
      this.$message({
        message: "保存成功",
        type: "success"
      });
      this.$router.push("/goods/list");
    }
  },
  created() {
    this.fetchCategory();
    this.id && this.fetch();
  },
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px !important;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>